#include "../mqserver/binding.hpp"
#include <gtest/gtest.h>

BindingManager::ptr bmp;

class BindingTest : public testing::Environment {
public:
    virtual void SetUp() override {
        bmp = std::make_shared<BindingManager>("./data/meta.db");
    }
    virtual void TearDown() override {
        //bmp->clear();
    }
};

TEST(BindingTest, insert_test){
    bmp->Bind("exchange1", "queue1", "news.music.#", true);
    bmp->Bind("exchange1", "queue2", "news.sport.#", true);
    bmp->Bind("exchange2", "queue1", "news.music.pop", true);
    bmp->Bind("exchange2", "queue2", "news.sport.football", true);
    bmp->Bind("exchange4", "queue4", "news.gossip.#", true);

    ASSERT_EQ(bmp->size(), 5);
}

TEST(BindingTest, select_test){
    ASSERT_EQ(bmp->exists("exchange1", "queue1"), true);
    ASSERT_EQ(bmp->exists("exchange1", "queue2"), true);
    ASSERT_EQ(bmp->exists("exchange2", "queue1"), true);
    ASSERT_EQ(bmp->exists("exchange2", "queue2"), true);

    Binding::ptr bp = bmp->getBinding("exchange1", "queue1");
    ASSERT_NE(bp.get(), nullptr);
    ASSERT_EQ(bp->exchange_name, "exchange1");
    ASSERT_EQ(bp->msgqueue_name, "queue1");
    ASSERT_EQ(bp->binding_key, "news.music.#");
}

TEST(BindingTest, select_exchange_test){
    MsgQueueBindingMap mqbm = bmp->getExchangeBindings("exchange1");
    ASSERT_EQ(mqbm.size(), 2);
    ASSERT_NE(mqbm.find("queue1"), mqbm.end());
    ASSERT_NE(mqbm.find("queue2"), mqbm.end());
}

TEST(BindingTest, remove_queue_test){
    bmp->removeMsgQueueBindings("queue1");
    ASSERT_EQ(bmp->exists("exchange1","queue1"), false);
    ASSERT_EQ(bmp->exists("exchange2","queue1"), false);
}

TEST(BindingTest, remove_exchange_test){
    bmp->removeExchangeBindings("exchange1");
    ASSERT_EQ(bmp->exists("exchange1","queue2"), false);
}

TEST(BindingTest, remove_single_test){
    ASSERT_EQ(bmp->exists("exchange2","queue2"), true);
    bmp->unBind("exchange2","queue2");
    ASSERT_EQ(bmp->exists("exchange2","queue2"), false);

    ASSERT_EQ(bmp->exists("exchange3","queue3"), false);

}

int main(int argc, char* argv[]){
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    RUN_ALL_TESTS();
    return 0;
}